using System;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using Resources;

public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SearchResults.EnableCaching = false;
        StringBuilder clientSideScript = new StringBuilder();
        clientSideScript.Append("<script language=JavaScript> ");
        clientSideScript.Append("function setLookup( FirstName, LastName, contactId)");
        clientSideScript.Append("{");
        clientSideScript.Append("var lookupData = new Array(); ");
        clientSideScript.Append("var lookupItem = new Object();  ");
        clientSideScript.Append("lookupItem.id = contactId ;  ");
        clientSideScript.Append("lookupItem.name= FirstName + ' ' + LastName ;");
        clientSideScript.Append("lookupItem.typename='contact';");
        clientSideScript.Append("lookupData[0] = lookupItem; ");
        clientSideScript.Append("parent.document.crmForm.customerid.DataValue = lookupData;");
        clientSideScript.Append(
            "parent.document.crmForm.all.IFRAME_SearchFrame.parentElement.parentElement.parentElement.style.display = 'none';");
        clientSideScript.Append("}");
        clientSideScript.Append("</script>");


        if (!Page.ClientScript.IsClientScriptBlockRegistered(clientSideScript.ToString()))
        {
            Page.ClientScript.RegisterClientScriptBlock(GetType(), "script", clientSideScript.ToString());
        }
        ;
    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int resourceIdentifierColumn = int.Parse(Resource.SearchResultsSystemIdentifier);
        string systemIdentifier = GridView1.SelectedRow.Cells[resourceIdentifierColumn].Text;
        string emailAddress = GridView1.SelectedRow.Cells[int.Parse(Resource.SearchResultsEmail)].Text;

        if (systemIdentifier.Length == 32)
        {
            CombinedSearch combinedSearch;
            Contact importContact = new Contact();
            importContact.ShopperId = systemIdentifier;
            importContact.CreateInCrm();
            LinkButton linkButton = (LinkButton) GridView1.SelectedRow.Cells[0].Controls[0];
            linkButton.OnClientClick = scriptToAttachToClientSideButtonClick(GridView1.SelectedRow);
            Search.Text = emailAddress;
            GridView1.DataBind();
        }
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string systemIdentifier = e.Row.Cells[int.Parse(Resource.SearchResultsSystemIdentifier)].Text;
            if (systemIdentifier.Length == 38)
            {
                LinkButton SelectButton = (LinkButton) e.Row.Cells[0].Controls[0];
                SelectButton.PostBackUrl = scriptToAttachToClientSideButtonClick(e.Row);
                SelectButton.OnClientClick = scriptToAttachToClientSideButtonClick(e.Row);
            }
            else
            {
                LinkButton SelectButton = (LinkButton) e.Row.Cells[0].Controls[0];
                SelectButton.Text = "Import";
                SelectButton.ToolTip =
                    "Pressing this button will import this contact from KMS into CRM, you then will need to click it again to select it";
            }
        }
    }

    private static string scriptToAttachToClientSideButtonClick(GridViewRow row)
    {
        StringBuilder javascriptToAppend = new StringBuilder();
        javascriptToAppend.AppendFormat("javascript:setLookup('{0}','{1}','{2}');",
                                        row.Cells[int.Parse(Resource.SearchResultsFirstName)].Text,
                                        row.Cells[int.Parse(Resource.SearchResultsLastName)].Text,
                                        row.Cells[int.Parse(Resource.SearchResultsSystemIdentifier)].Text);
        return javascriptToAppend.ToString();
    }
}